<?php	
	
	require_once("Joob/Utils/Helper.php");

	final class Joob_Utils_Utils{
		
		/**
		 * @desc Transform an array, using a mapping.
		 * @param $a The Array used in transformation
		 * @param $fn The function to apply to each element.
		 * @param $change_local The flag indicates whether we should make change on the
		 * passed array.
		 */
		final public static function transform(&$a, $fn, $change_local=false){
			if ($change_local==true){
				for ($i=0; $i<count($a); $i++){
					$a[]=$fn($a[$i]);
				}
				return $a;
			}else{
				$b=array();
				for ($i=0; $i<count($a); $i++){
					$b[]=$fn($a[$i]);
				}
				return $b;
			}
		} 
		
		/**
		 * @desc Return the ip address of the current request.
		 */
		public static function ip(){
			return getIP();	
		}
		
		
		/**
		 * @desc Create a secret code based on the session and
		 * a locally stored constant. Default constant is SECRET_KEY.
		 * @param const string $private
		 */
		public static function makeCode($private=SECRET_KEY){
			return md5(Session::id()."-".$private); 
		}
        
		
		/**
		 * @desc Check that the code is a valid string generated 
		 * by makeCode(). The two methods must share the same 
		 * constant $private.
		 * @param string $code
		 * @param const string $private
		 */
		function checkCode($code, $private=SECRET_KEY){
			if ($code!=md5(Session::id()."-".$private)) return false;
			return true;
		}
		
		
        /**
        * @desc Serialize an array and change to a string.
        * @return String 
        */
        public static function serialize(&$a){
            confirm(count($a)<=200,'array too long');
            $text="";
            for ($i=0; $i<count($a); $i++){
                $a[$i]=str_replace("|","&#124;",$a[$i]);
                $text.="|".$a[$i];
            }
            return $text;
        }
        
        
        /**
        * @desc Unserialize a string which is serialized by the 
        * serialize method.
        * @return array
        */
        public static function unserialize(&$str){
            $a=Joob_Utils_Word::split("|",$str);
            return $a;
        }
        
        
			
		/**
		 * @desc Get the initial part of the array, started at $initial;
		 * If $last is out of range, we make it to the maximal length (length of the array)
		 * @param Array $array
		 * @param Integer $initial
		 * @param Integer $last (Exclusive)
		 */
		public static function subArray(&$array, $initial=0, $last=0){
			$result=array();
			
			if ($last==0 || $last>=count($array)){
				$last=count($array);
			}
			for ($i=$initial; $i<$last; $i++){
				$result[]=$array[$i];
			}
			
			return $result;
		}
		
        
        /**
        * @desc Apply a map to every member of an array.
        * @example:
        */
        public static function map(&$a, $fn){
            for ($i=0; $i<count($a); $i++){
                $a[$i]=call_user_func($fn,$a[$i]);
            }    
        }
        

		
		/**
		 * @desc Redirect the request to another links.
		 * @param URL $link
		 * @param Boolean $auto
		 */
		public static function redirect(){
			$args=func_get_args();
			$link=implode("/",$args);
			if (Joob_Utils_Word::initial($link,4)=='http'){
				return redirect($link);
			}else{
				exit();
			}
		}
		
		
		# Function: fileExt($file)
		# ----------------------------------
		# Get file extension of a file.
		
		function fileExt($file){
			$l=Joob_Utils_Word::split('.',$file);
			if (count($l)<=1) return false;
			return $l[count($l)-1];
		}
		
		# Function: fileExists($file)
		# ----------------------------------
		# Check if a file exists.
		
		function fileExists($file){
			return file_exists($file);
		}
		
		
		# Function: date()
		# ----------------------------------
		function date($time){
			return date('D, M d, Y',$time);
		}
		
		
        public static function hash($key){
            return md5($key."con.meo.cazoodle.dep.trai");
        }
        
        
        /**
        * @desc Create friendly date based on timestamp.
        */
        public static function friendlyDate($time){
            return date("d-m-Y",$time);
        }
        
        
        /**
        * @desc Create friendly date based on timestamp.
        */
        public static function friendlyDateTime($time){
            return date("H:i:s d-m-Y",$time);
        }
        
		
        
        public static function trueEmail($email){
        	return str_replace('&#64;','@',$email);
        }
	}
	
?>